package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
@Transactional
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public void insert(SetmealDTO setmealDTO) {
        //新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //设置默认停售状态
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insert(setmeal);

        //新增套餐菜品关联数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(sd->sd.setSetmealId(setmeal.getId()));
        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult selectAll(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.selectAll(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据Id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {
        //查询套餐
        SetmealVO setmealVO = setmealMapper.selectById(Arrays.asList(id)).get(0);
        //查询套餐菜品关系
        List<SetmealDish> listSD = setmealDishMapper.selectById(id);
        //合并
        setmealVO.setSetmealDishes(listSD);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        // 修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除套餐包含的菜品
        setmealDishMapper.delete(setmeal.getId());
        //添加套餐包含的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(sd->sd.setSetmealId(setmeal.getId()));
            setmealDishMapper.insert(setmealDishes);
        }
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Override
    public void delete(List<Long> ids) {
        //需要判断是否有起售的套餐
        List<SetmealVO> setmealVOS = setmealMapper.selectById(ids);
        for (SetmealVO setmealVO : setmealVOS) {
            if (setmealVO.getStatus() == StatusConstant.ENABLE){
                //如果有,抛出异常
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //如果没有,继续删除
        setmealMapper.deleteAll(ids);
    }

    /**
     * 套餐起售,停售
     * @param status
     * @param id
     * @return
     */
    @Override
    public void setStatus(Integer status, Long id) {
        if (status.equals(StatusConstant.ENABLE) ){
            //获取该套餐包含的菜品 select d.* from dish d,setmeal_dish s where dish_id = d.id and setmeal_id = #{id};  select dish.* from dish where id in (select dish_id from setmeal_dish where setmeal_id = #{id})
            List<Dish> listDish = setmealDishMapper.selectDishById(id);
            //遍历判断是否有停售菜品
            listDish.forEach(ld->{
                if (ld.getStatus().equals(StatusConstant.DISABLE) ){
                    //如果有,就抛异常
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        //如果不是起售或者没有停售的菜品,就正常运行
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类Id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> selectId(Long categoryId) {
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);
        List<Setmeal> setmealList = setmealMapper.selectId(setmeal);
        return setmealList;
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> selectDish(Long id) {
        List<DishItemVO> list = setmealDishMapper.selectDish(id);
        return list;
    }
}
